package com.commerce.img.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.commerce.img.MyAppData;
import com.commerce.img.R;
import com.commerce.img.adapter.MyImgAdapter;
import com.commerce.img.entity.DataUrl;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private MyAppData myAppData;
    private EditText editText;
    private MyImgAdapter adapter;
    private List<DataUrl.ImgdataBean> data=new ArrayList<>();
    List<DataUrl.ImgdataBean> list= new ArrayList<>();
    RecyclerView recycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startActivity(new Intent(MainActivity.this,WelcomeActivity.class));
        myAppData = (MyAppData) getApplication();
        recycler = findViewById(R.id.mn_rcv);
        Button search = findViewById(R.id.mn_btn_search);
        editText = findViewById(R.id.mn_et_search);
        Button type1 = findViewById(R.id.mn_type1);
        Button type2 = findViewById(R.id.mn_type2);
        Button type3 = findViewById(R.id.mn_type3);
        Button local = findViewById(R.id.mn_l);
        data = myAppData.getList();
        dataFilter("",true);
        search.setOnClickListener(this);
        type1.setOnClickListener(this);
        type2.setOnClickListener(this);
        type3.setOnClickListener(this);
        local.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.mn_btn_search :
                dataFilter(editText.getText().toString(),true);
                break;
            case R.id.mn_type1:
                dataFilter("1",false);
                break;
            case R.id.mn_type2 :
                dataFilter("2",false);
                break;
            case R.id.mn_type3:
                dataFilter("3",false);
                break;
            case R.id.mn_l:
                dataFilter("",true);
                break;
        }
    }

    /**
     *搜索功能和分类功能实现
     * @param type 类型或者关键词
     * @param b    关键词（true）类型（false）
     */
    public void dataFilter(String type,boolean b) {
        list.clear();
        if(b){
            for (int i=0;i<data.size();i++){
                if(data.get(i).getImgName().contains(type)){ list.add(data.get(i)); } }
        }else {
            for (int i=0;i<data.size();i++){
                if(data.get(i).getImgtye().equals(type)){ list.add(data.get(i)); } }
        }
        if(adapter!=null){
            adapter.notifyDataSetChanged();
        }else {
            adapter = new MyImgAdapter(MainActivity.this,list,myAppData);
            //
            recycler.setLayoutManager(new StaggeredGridLayoutManager(3, DividerItemDecoration.VERTICAL));
            recycler.setAdapter(adapter);
        }

    }

}